package com.xgry.bpm.api.base.carapply.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgry.bpm.api.base.carapply.dao.CarApplyDao;
import com.xgry.bpm.api.base.carapply.entity.CarApply;
import com.xgry.bpm.api.base.carapply.form.CarApplyForm;
import com.xgry.bpm.api.base.carapply.service.CarApplyService;
import com.xgry.bpm.api.base.carapply.vo.CarApplyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * CarApply服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-16
 */
@Service
        public class CarApplyServiceImpl extends ServiceImpl<CarApplyDao, CarApply>implements CarApplyService {

        @Override
        public int save(CarApplyForm record){
        CarApply data=this.convertDO(record);
            data.setCreatedTime(new Date());
            return baseMapper.insert(data);
        }

        @Override
        public int updateById(CarApplyForm record){
        //修改时间
    CarApply data=this.convertDO(record);
        return baseMapper.updateById(data);
        }

        @Override
        public int deleteById(String id){
        return baseMapper.deleteById(id);
        }

        @Override
        public CarApplyVO selectById(String id){
        CarApply data=baseMapper.selectById(id);
        CarApplyVO vo=new CarApplyVO();
        BeanUtils.copyProperties(data,vo);
        return vo;
        }

        @Override
        public List<CarApplyVO>selectAll(CarApplyForm record){
    CarApply data=this.convertDO(record);
        QueryWrapper<CarApply>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<CarApply>list=baseMapper.selectList(queryWrapper);
        return this.convert(list);
        }

        @Override
        public IPage<CarApplyVO>selectPage(CarApplyForm record){
        // form -> do 转换
    CarApply data=this.convertDO(record);
        // 分页数据设置
        Page<CarApply>page=new Page<>(record.getCurrent(),record.getSize());
        // 查询条件
        QueryWrapper<CarApply>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        IPage<CarApply>list=baseMapper.selectPage(page,queryWrapper);
        IPage<CarApplyVO>iPage=new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
        }

        @Override
        public IPage<CarApply>getPage(IPage<CarApply>page,QueryWrapper<CarApply>queryWrapper){
        return baseMapper.selectPage(page,queryWrapper);
        }

    @Override
    public IPage<CarApplyVO> CarApplyPage(CarApplyForm record) {
        // form -> do 转换
        CarApply data=this.convertDO(record);
        // 分页数据设置
        Page<CarApply>page=new Page<>(record.getCurrent(),record.getSize());
        // 查询条件
        QueryWrapper<CarApply>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        queryWrapper.eq("isDel",0);
        IPage<CarApply>list=baseMapper.selectPage(page,queryWrapper);
        IPage<CarApplyVO>iPage=new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
    }


    /**
        * Form -> Do
        * @param form 对象
        * @return Do对象
        */
        private CarApply convertDO(CarApplyForm form){
            CarApply data=new CarApply();
            BeanUtils.copyProperties(form,data);
        return data;
        }

        /**
        * Do -> VO
        * @param list 对象
        * @return VO对象
        */
        private List<CarApplyVO>convert(List<CarApply>list){
        List<CarApplyVO> carApplyList=new ArrayList<>();
        if(CollectionUtils.isEmpty(list)){
        return carApplyList;
        }
        for(CarApply source:list){
    CarApplyVO target=new CarApplyVO();
        BeanUtils.copyProperties(source,target);
    carApplyList.add(target);
        }
        return carApplyList;
        }

        }
